﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ReporterEngine
{
    public class XMLTemplate
    {
        public TemplateNode Parse(XElement source)
        {
            TemplateNode root = new TemplateNode();

            StringBuilder sb = new StringBuilder();

            trace(source, root);
            
            
            return root;
        }

        private int lastPos = 0;

        void trace(XElement element, TemplateNode node)
        {
            //validate element

            if(element.Elements().Count() == 0 && element.Value.Contains("{"))
            {
                //find end command }
                //var endElement = findEndCommand(element);



                TemplateNode newTemplateNode = new TemplateNode();

                node.Childs.Add(newTemplateNode);
            }

            foreach(var child in element.Elements())
            {
                trace(child, node);
            }
        }

        XElement findEndCommand(XElement element)
        {
            foreach(var el in element.Descendants())
            {
                if (el.Elements().Count() == 0 && el.Value.Contains("}"))
                    return el;
            }
            
            return null;
        }
    }
}
